package erfen;

import java.util.Arrays;

//二分查找
public class erfen {
    public static void main(String[] args) {
        int[] arr = {1,2,5,7,8,9,};
        int i = get(9, arr);
        int j = Arrays.binarySearch(arr,7);
        System.out.println(j);
        System.out.println(i);
        System.out.println(i);
    }

    private static int get(int i, int[] arr) {
        int min = 0;
        int max = arr.length - 1;
        while (min <= max) {

            int mid = (min + max) / 2;
            if (arr[mid] > i) {
                max = mid - 1;
            } else if (arr[mid] < i) {
                min = mid + 1;
            } else {
                return mid;
            }
        }
        return -1;
    }
}
